package MULE.Model.Panel;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import MULE.Model.*;

/**
 * This class creates the JPanel that will display the Map object.
 * 
 * 
 * @ author Aaron Englebardt @ version 1.0 10.16.2013
 * 
 */
public class MapPanel extends JPanel {

	// Map object displayed in the panel.
	private Map gameMap;

	/**
	 * Constructor for the JPanel displaying the map type
	 * 
	 * @param m
	 *            Where the map that will be displayed is passed in.
	 */
	public MapPanel(Map m) {
		// Save map reference.
		this.gameMap = m;
		// Set panel layout.
		setLayout(new GridLayout(Map.ROWS, Map.COLUMNS, 0, 0));
		// Make the panel's border color match the default tile border color.
		setBorder(BorderFactory.createLineBorder(Tile.BORDER_COLOR));

		// Fill panel with the map's tiles.
		for (int x = 0; x < Map.ROWS; x++) {
			for (int y = 0; y < Map.COLUMNS; y++) {
				add(gameMap.getTile(x, y));
			}
		}
	}

	/**
	 * Returns a reference to this panel's map object.
	 * 
	 * @return Reference to this panel's map object.
	 */
	public Map getMap() {
		return gameMap;
	}

	/**
	 * Adds the given action listener to the Town tile. Removes any existing
	 * action listeners from the Town tile.
	 * 
	 * @param newListener
	 *            The Town tile's new action listener.
	 */
	public void addTownListener(ActionListener newListener) {
		Tile town = gameMap.getTown();
		// Remove existing listeners.
		ActionListener[] listeners = town.getActionListeners();
		for (ActionListener listener : listeners) {
			town.removeActionListener(listener);
		}
		// Add new listener.
		town.addActionListener(newListener);
	}

	/**
	 * Applies the given action listener to all resource tiles on map. Any
	 * existing listeners are removed from resource tiles. This does not affect
	 * the Town.
	 * 
	 * @param newListener
	 *            The resource tiles' new action listener.
	 */
	public void setResourceTileListener(ActionListener newListener) {
		// Iterate through all map tiles.
		for (int x = 0; x < Map.ROWS; ++x) {
			for (int y = 0; y < Map.COLUMNS; ++y) {
				// Don't touch the Town.
				if (x == Map.CENTER_ROW - 1 && y == Map.CENTER_COLUMN - 1) {
					continue;
				}
				// Remove all action listeners from this resource tile.
				Tile resourceTile = gameMap.getTile(x, y);
				ActionListener[] listeners = resourceTile.getActionListeners();
				for (ActionListener listener : listeners) {
					resourceTile.removeActionListener(listener);
				}
				// Add new listener to this resource tile.
				resourceTile.addActionListener(newListener);
			}
		}
	}

	/**
	 * Sets all of the resource tile buttons' action commands.
	 * 
	 * @param actionCommand
	 *            The new action command for all resource tiles.
	 */
	public void setResourceTileActionCommand(String actionCommand) {
		// Iterate through all map tiles.
		for (int x = 0; x < Map.ROWS; ++x) {
			for (int y = 0; y < Map.COLUMNS; ++y) {
				// Don't touch the Town.
				if (x == Map.CENTER_ROW - 1 && y == Map.CENTER_COLUMN - 1) {
					continue;
				}
				// Set the resource tile's action command.
				Tile resourceTile = gameMap.getTile(x, y);
				resourceTile.setActionCommand(actionCommand);
			}
		}
	}

	public void setKeyListener(KeyListener listener) {
		KeyListener[] listeners = this.getKeyListeners();
		for (KeyListener l : listeners) {
			this.removeKeyListener(l);
		}
		this.addKeyListener(listener);
	}

	public void addNotify() {
		super.addNotify();
		this.requestFocusInWindow();
	}
}
